/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "物体编辑.h"
//#include "3D控制手柄.h"
#include "编辑骨骼.h"



void f_OE_开始拾取点初始化(S_摄像机* camera, const vec2& 鼠标局部位置, const S_Rect2Df& rect) {
	vec3 坐标 = { 鼠标局部位置.x, 鼠标局部位置.y, 0.0f };
	vec3 当前射线[2];
	当前射线[0] = S_框架::g_Scene->f_get屏幕坐标到空间(camera, 坐标, rect);
	坐标.z = 1.0;
	当前射线[1] = S_框架::g_Scene->f_get屏幕坐标到空间(camera, 坐标, rect);
	S_射线拾取参数::g结束射线 = vec_normalize2(当前射线[1] - 当前射线[0]);

	vec3 视线向量 = f_camera_get视线向量(camera);
	switch (S_框架::g_变换轴) {
	case E_轴向::e_X: vec_mul(&视线向量, vec3{ 0, 1, 1 }); break;
	case E_轴向::e_Y: vec_mul(&视线向量, vec3{ 1, 0, 1 }); break;
	case E_轴向::e_Z: vec_mul(&视线向量, vec3{ 1, 1, 0 }); break;
	default:
		break;
	}
	vec_normalize(视线向量);

	f_graph_射线相交平面(当前射线[0] - S_射线拾取参数::g手柄位置, 当前射线[1] - S_射线拾取参数::g手柄位置, 视线向量, S_射线拾取参数::g交点S);
}





void f_OE_准备移动(C_Widget* self, S_摄像机* camera) {
	S_框架::g_变换开关 = true;
	S_框架::g_变换轴 = E_轴向::e_XYZ;
	S_框架::g_变换方式 = E_变换::e_移动;
	S_框架::g_视线向量 = f_camera_get视线向量(camera);
	S_射线拾取参数::g手柄位置 = S_框架::g_变换手柄->f_get位置();
	
	S_框架::g_鼠标起始坐标 = f_widget_get鼠标局部位置(C_Widget::g_鼠标.gPos, self);
	f_OE_开始拾取点初始化(camera, S_框架::g_鼠标起始坐标, f_ui_get视口(self));
	
}

void f_OE_准备旋转(C_Widget* self, S_摄像机* camera) {

}

void f_OE_准备缩放(C_Widget* self, S_摄像机* camera) {

}

void f_OE_准备变换(std::vector<S_物体*>& obs, uint32 变换类型) {
	S_物体::g_更新曲线关键帧 = false;
}



void f_OE_骨骼编辑模式切换(S_Props& prop) {
	switch (f_prop_enum(prop)) {
	case E_骨骼编辑模式::e_物体模式: {
		f_NE_骨骼物体状态颜色设置(S_节点数据::g_选中激活物体);
		break;
	}
	case E_骨骼编辑模式::e_姿态模式:
	case E_骨骼编辑模式::e_骨节编辑模式: {
		f_NE_骨骼编辑状态颜色设置(S_节点数据::g_选中激活物体);
		break;
	}
	default:
		break;
	}
}

void f_OE_移动后更新(std::vector<S_物体*>& obs) {
	//auto& planes = f_global_get激活节点();
	auto& planes = f_NodeCtx_从可视物体取节点(obs);
	for (auto& e : planes) {
		e->f_set是否要更新(true);
	}


	if (f_prop_Bool(S_节点数据状态::g_打开编辑模式) == false) return;

	switch (S_框架::g当前编辑对象类型) {
	case E_物体类型::t_网格:
		break;
	case E_物体类型::t_骨架:
	case E_物体类型::t_骨骼:
		for (auto& e : obs) f_BE_从骨节更新骨骼(e);
		break;
	case E_物体类型::t_灯光:

		break;
	default:
		break;
	}

	
}

void f_OE_旋转后更新(std::vector<S_物体*>& obs) {
	//auto& planes = f_global_get激活节点();
	auto& planes = f_NodeCtx_从可视物体取节点(obs);
	for (auto& e : planes) {
		e->f_set是否要更新(true);
	}

	if (f_prop_Bool(S_节点数据状态::g_打开编辑模式) == false) return;

	switch (S_框架::g当前编辑对象类型) {
	case E_物体类型::t_网格:

		break;
	case E_物体类型::t_骨架:
	case E_物体类型::t_骨骼:
		for (auto& e : obs) f_BE_从骨节更新骨骼(e);
		break;
	case E_物体类型::t_灯光:

		break;
	default:
		break;
	}
}

void f_OE_缩放后更新(std::vector<S_物体*>& obs) {
	//auto& planes = f_global_get激活节点();
	auto& planes = f_NodeCtx_从可视物体取节点(obs);
	for (auto& e : planes) {
		e->f_set是否要更新(true);
	}

	if (f_prop_Bool(S_节点数据状态::g_打开编辑模式) == false) return;
}

void f_OE_物体变换插帧(uint32 变换类型) {
	S_时间帧& 帧 = f_NodeCtx_get帧();
	int32 tiem = f_prop_I32(帧.当前帧);


	vec2 value;
	value.x = tiem;

	S_RGBA8UI color[3] = {
		{255,60,60,255},
		{60,255,60,255},
		{60,60,255,255},
	};
	if (变换类型 & 1) {
		for (auto& e : S_节点数据::g_选中激活物体) {
			if (!(e->m_位置)) e->m_位置 = f_an_创建曲线通道(3);

			float32* v = (float32*)&(e->m_变换.location);
			for (int32 i = 0; i < e->m_位置->count; ++i) {
				uint32 id = e->m_位置->曲线[i]->count;

				value.y = v[i];
				//vec2 value_L;
				//vec2 value_R;
				int32 重叠索引 = f_an_查找重叠帧(e->m_位置->曲线[i], value.x);
				if (重叠索引 < 0) {
					f_an_添加曲线点(e->m_位置->曲线[i], value, {}, {}, id, color[i]);
				}
				else {
					e->m_位置->曲线[i]->point[重叠索引].value = value;
				}
				f_an_曲线值排序(e->m_位置->曲线[i]);
				f_an_自动控制点(e->m_位置->曲线[i]);
			}
			e->m_isUpdate = true;
		}
	}

	if (变换类型 & 2) {
		for (auto& e : S_节点数据::g_选中激活物体) {
			if (!(e->m_旋转)) e->m_旋转 = f_an_创建曲线通道(3);

			float32* v = (float32*)&(e->m_变换.rotation);
			for (int32 i = 0; i < e->m_旋转->count; ++i) {
				uint32 id = e->m_旋转->曲线[i]->count;

				value.y = v[i];
				int32 重叠索引 = f_an_查找重叠帧(e->m_旋转->曲线[i], value.x);
				if (重叠索引 < 0) {
					f_an_添加曲线点(e->m_旋转->曲线[i], value, {}, {}, id, color[i]);
				}
				else {
					e->m_旋转->曲线[i]->point[重叠索引].value = value;
				}
				f_an_曲线值排序(e->m_旋转->曲线[i]);
				f_an_自动控制点(e->m_旋转->曲线[i]);
			}
			e->m_isUpdate = true;
		}
	}

	if (变换类型 & 4) {
		for (auto& e : S_节点数据::g_选中激活物体) {
			if (!(e->m_缩放)) e->m_缩放 = f_an_创建曲线通道(3);

			float32* v = (float32*)&(e->m_变换.scale);
			for (int32 i = 0; i < e->m_缩放->count; ++i) {
				uint32 id = e->m_缩放->曲线[i]->count;

				value.y = v[i];
				int32 重叠索引 = f_an_查找重叠帧(e->m_缩放->曲线[i], value.x);
				if (重叠索引 < 0) {
					f_an_添加曲线点(e->m_缩放->曲线[i], value, {}, {}, id, color[i]);
				}
				else {
					e->m_缩放->曲线[i]->point[重叠索引].value = value;
				}
				f_an_曲线值排序(e->m_缩放->曲线[i]);
				f_an_自动控制点(e->m_缩放->曲线[i]);
			}
			e->m_isUpdate = true;
		}
	}


	switch (变换类型) {
	case E_变换::e_移动: {
		
		break;
	}
	case E_变换::e_旋转: {
		
		break;
	}
	case E_变换::e_缩放: {
		
		break;
	}
	default:
		break;
	}
}














